package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShopppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R list() {
        LambdaQueryWrapper<ShoppingCart> qwshop = new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();

        qwshop.eq(ShoppingCart::getUserId, userId);
        //qwshop.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(qwshop);

        return R.success(list);
    }

    @PostMapping("/add")
    public R addShoppingCart(@RequestBody ShoppingCart shoppingCart) {

        log.info("购物车数据：{}", shoppingCart);
        //设用户id，指定当前时那个用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> qwshop = new LambdaQueryWrapper<>();
        //查询用户
        qwshop.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        if (dishId != null) {//添加到购物车的是菜品
            qwshop.eq(ShoppingCart::getDishId, dishId);
        } else {//添加到购物车的是套餐
            qwshop.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //第一次点时新增，后面都是修改
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart one = shoppingCartService.getOne(qwshop);
        if (one == null) {
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        } else {
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }


    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart) {

        LambdaQueryWrapper<ShoppingCart> qwshop = new LambdaQueryWrapper<>();
        qwshop.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            qwshop.eq(ShoppingCart::getDishId, dishId);
        } else {
            qwshop.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(qwshop);

        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if (number > 0) {
            shoppingCartService.updateById(one);
        } else {
            shoppingCartService.remove(qwshop);
        }
        return R.success(one);
    }

    @DeleteMapping("/clean")
    public R cleanall() {

        shoppingCartService.cleanall();
        return R.success("清除成功");

    }
}
